<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="嘉宾姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入嘉宾姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="头衔" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入头衔"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" />
      <el-table-column label="姓名" prop="name" />
      <el-table-column label="手机号码" prop="mobile" />
      <el-table-column label="单位" prop="unit" />
      <el-table-column label="科室" prop="department" />
      <el-table-column label="照片" prop="photoUrl">
        <template slot-scope="scope">
          <el-image v-if="scope.row.photoUrl" :src="scope.row.photoUrl" fit="fill" :preview-src-list="[scope.row.photoUrl]" style="width: 20px; height: 20px" />
        </template>
      </el-table-column>
      <el-table-column label="头衔" prop="title" />
      <el-table-column label="头衔2" prop="title2" />
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="form" label-width="90px" style="height: 500px; overflow: auto">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓" clearable />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" clearable />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" clearable />
        </el-form-item>
        <el-form-item label="科室" prop="department">
          <el-input v-model="form.department" placeholder="请输入科室" clearable />
        </el-form-item>
        <el-form-item label="头衔" prop="title">
          <el-input v-model="form.title" placeholder="请输入头衔" clearable />
        </el-form-item>
        <el-form-item label="头衔2" prop="title2">
          <el-input v-model="form.title2" placeholder="请输入头衔2" clearable />
        </el-form-item>
        <el-form-item label="照片">
          <el-upload
            :action="uploadImgUrl"
            list-type="picture-card"
            :multiple="false"
            :limit="1"
            :headers="headers"
            :file-list="fileList"
            :before-upload="beforeUpload"
            :on-success="handleUploadSuccess"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" clearable />
        </el-form-item>
        <el-form-item label="嘉宾简介">
          <editor v-model="form.profile" :min-height="192"/>
        </el-form-item>
        <el-form-item label="介绍页背景板">
          <el-upload
            :action="uploadImgUrl"
            list-type="picture-card"
            :multiple="false"
            :limit="1"
            :headers="headers"
            :file-list="fileList2"
            :before-upload="beforeUpload2"
            :on-success="handleUploadSuccess2"
            :on-remove="handleRemove2">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {listExpertr, addExpert, delExpert, updateExpert, getExpert} from "@/api/meetingManagement/guest";
import { VueCropper } from "vue-cropper"
import { getToken } from "@/utils/auth";

export default {
  name: "Guest",
  dicts: ['sys_normal_disable'],
  components: { VueCropper },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        title: undefined,
        mobile: undefined
      },
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      // 表单参数
      form: {},
      fileList: [],
      fileList2: [],
      uploadImgUrl: process.env.VUE_APP_BASE_API + '/common/upload', // 上传的图片服务器地址
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        department: [
          { required: true, message: "科室不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "头衔不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确的手机号码格式",
            trigger: "blur"
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true
      listExpertr(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.roleList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        title: null,
        mobile: null,
        unit: null,
        department: null,
        photoUrl: null,
        backgroundUrl: null,
        remark: null,
        profile: null
      }
      this.fileList = []
      this.fileList2 = []
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加演讲嘉宾"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const roleId = row.roleId || this.ids
      getExpert(roleId).then(response => {
        this.form = response.data
        // 设置照片回显
        if (this.form.photoUrl) {
          this.fileList = [{
            name: 'photo',
            url: this.form.photoUrl
          }]
        }
        // 设置背景图回显
        if (this.form.backgroundUrl) {
          this.fileList2 = [{
            name: 'background',
            url: this.form.backgroundUrl
          }]
        }
        this.open = true
      })
      this.title = "修改演讲嘉宾"
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateExpert(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addExpert(this.form).then(response => {
              this.$modal.msgSuccess("添加成功");
              this.open = false
              this.getList()
            });
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guestId = row.id
      this.$modal.confirm('是否确认删除讲者id为"' + guestId + '"的数据项？').then(function() {
        return delExpert(guestId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    beforeUpload(file) {
      let imageUrl = URL.createObjectURL(file);
      const isJPG = file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 3;
      if (!isJPG) {
        this.$message.error('上传图片只能是 png/JPG/gif 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 3MB!');
      }
      return isJPG && isLt2M;
    },
    handleRemove() {
      this.form.photoUrl = null
      this.fileList = []
    },
    handleUploadSuccess(res, file) {
      if (res && res.data && res.data.path) {
        this.form.photoUrl = res.data.path;
        this.fileList = [{ name: file.name, url: res.data.path }]
      } else if (res && res.url) {
        this.form.photoUrl = res.url;
        this.fileList = [{ name: file.name, url: res.url }]
      }
    },
    beforeUpload2(file) {
      let imageUrl = URL.createObjectURL(file);
      const isJPG = file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 3;
      if (!isJPG) {
        this.$message.error('上传图片只能是 png/JPG/gif 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 3MB!');
      }
      return isJPG && isLt2M;
    },
    handleRemove2() {
      this.form.backgroundUrl = null
      this.fileList2 = []
    },
    handleUploadSuccess2(res, file) {
      if (res && res.data && res.data.path) {
        this.form.backgroundUrl = res.data.path;
        this.fileList2 = [{ name: file.name, url: res.data.path }]
      } else if (res && res.url) {
        this.form.backgroundUrl = res.url;
        this.fileList2 = [{ name: file.name, url: res.url }]
      }
    }
  }
}

</script>
<style scoped lang="scss">

</style>
